package com.justerdu.mvpclient.user.view;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.justerdu.mvpclient.R;
import com.justerdu.mvpclient.user.model.domain.User;
import com.justerdu.mvpclient.user.presenter.IUserService;
import com.justerdu.mvpclient.user.presenter.UserService;
import com.justerdu.mvpclient.util.DBOpenHandler;

import java.util.List;

/**
 * @Author JusterDu 2016/11/21.
 * @Func
 */
public class CrudActivity extends Activity implements IUserView,View.OnClickListener {

    private Button btnAdd;
    private Button btnDelete;
    private Button btnEdit;
    private Button btnFind;

    private IUserService userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crud);

        btnAdd = (Button) this.findViewById(R.id.btn_add);
        btnDelete = (Button) this.findViewById(R.id.btn_delete);
        btnEdit = (Button) this.findViewById(R.id.btn_edit);
        btnFind = (Button) this.findViewById(R.id.btn_find);

        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnEdit.setOnClickListener(this);
        btnFind.setOnClickListener(this);

        userService = new UserService(this);
        DBOpenHandler dbHandler = new DBOpenHandler(this, "mvpsqlite.db", null, 1);// 创建数据库文件
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        dbHandler.onCreate(db);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                User user = new User();
                user.setName("DuHang");
                user.setAge(20);
                user.setSex("男");
                userService.addUser(user);

                User user2 = new User();
                user.setName("JD");
                user.setAge(18);
                user.setSex("男");
                Boolean flag = userService.addUser(user2);
                if(flag){
                    Toast.makeText(this,"新增成功···", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"新增失败···", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_delete:
                Boolean flag1 = userService.deleteUser(1);
                if(flag1){
                    Toast.makeText(this,"删除成功···", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"删除失败···", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_edit:
                User user3 = userService.findUserById(1);
                user3.setSex("女");
                Boolean flag2 = userService.editUser(user3);
                if(flag2){
                    Toast.makeText(this,"修改成功···", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this,"修改失败···", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_find:
                List<User> list = userService.findAllUser();
                Toast.makeText(this,"查询结果： " + list.toString(), Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onAddBtn() {

    }

    @Override
    public void onDeleteBtn() {

    }

    @Override
    public void onEditBtn() {

    }

    @Override
    public void onFindAllBtn() {

    }
}
